<?php
if(!defined('IN_WEB')) {
	exit('Access Denied');
}

class ProductController extends Controller{
	function __construct(){
		parent::__construct();
		$this->productAction = new ProductAction();
		$this->taxonomyAction = new TaxonomyAction();
		$this->imageAction = new ImageAction();
	}
	
	function index(){
		$this->smarty->display('admin/product/index.tpl');
	}
	
	function items(){
		$this->smarty->assign('title', '产品列表');
		$this->smarty->display('admin/product/items.tpl');
	}
	
	function items_data(){
		$perPage = intval($_GET['iDisplayLength']);
		$pageStart = intval($_GET['iDisplayStart']);
		$sortColNum = intval($_GET['iSortCol_0']);
		$sortDir = $_GET['sSortDir_0'];
		
		$where = array();
		for($i=0; $i<=15; $i++){
		    if($_GET['sSearch_'.$i]){
    		    switch ($i){
    		        case 0:
    		            $where[] = " title LIKE '%".trim($_GET['sSearch_'.$i])."%'";
    		            break;
    		        case 1:
    		            $where[] = " source_id = ".trim($_GET['sSearch_'.$i]);
    		            break;
    		    }
		    
		        
		    }
		}
		
		switch($sortColNum){
			case 1:
				$sortCol = 'pid';
				break;
			case 5:
				$sortCol = 'sales';
				break;
			case 6:
				$sortCol = 'price';
				break;
			case 7:
				$sortCol = 'discount';
				break;
			case 11:
				$sortCol = 'fav';
				break;
			case 12:
				$sortCol = 'status';
				break;
			case 13:
				$sortCol = 'uid';
				break;
			case 14:
				$sortCol = 'created';
				break;
			default:
				$sortCol = 'pid';
		}
		$conditions = array(
	        'where' => $where,
			'from' => $pageStart,
			'count' => $perPage,
			'sort' => "`$sortCol` $sortDir",
		);
		$result = $this->productAction->productListJson($conditions);
		exit($result);
	}
	
	function edit(){
		$pid = intval($_REQUEST['pid']);
		if($pid){
			$product = $this->productAction->productGetFull($pid);
			
			$source = $product['terms'][TERM_MACHINE_NAME_SOURCE]['tid'];
			$ptype_first = $product['terms'][TERM_MACHINE_NAME_PTYPE_FIRST]['tid'];
			$ptype_second = $product['terms'][TERM_MACHINE_NAME_PTYPE_SECOND]['tid'];
			$ptype_third = $product['terms'][TERM_MACHINE_NAME_PTYPE_THIRD]['tid'];
			$color = $product['terms'][TERM_MACHINE_NAME_COLOR]['tid'];
			
			$terms = $this->taxonomyAction->termList($product['terms'][TERM_MACHINE_NAME_PTYPE_FIRST]['vid'], $ptype_first);
			$ptype_second_html = '';
			foreach($terms as $t){
				$selected = '';
				$t['tid'] == $ptype_second && $selected = 'selected';
				$ptype_second_html .= "<option value=\"{$t['tid']}\" $selected>{$t['name']}</option>";
			}
			
		    $terms = $this->taxonomyAction->termList($product['terms'][TERM_MACHINE_NAME_PTYPE_FIRST]['vid'], $ptype_second);
			$ptype_third_html = '';
			foreach($terms as $t){
				$selected = '';
				$t['tid'] == $ptype_third && $selected = 'selected';
				$ptype_third_html .= "<option value=\"{$t['tid']}\" $selected>{$t['name']}</option>";
			}
			
			$this->smarty->assign('product', $product);
			$this->smarty->assign('source', $source);
			$this->smarty->assign('ptype_first', $ptype_first);
			$this->smarty->assign('ptype_second_html', $ptype_second_html);
			$this->smarty->assign('ptype_third_html', $ptype_third_html);
			$this->smarty->assign('color', $color);
		}
		$sources = $this->taxonomyAction->getSourceTerms();
		$colors = $this->taxonomyAction->getColorTerms();
		$ptype_firsts = $this->taxonomyAction->getPtypeFirst();
		$this->smarty->assign('sources', $sources);
		$this->smarty->assign('colors', $colors);
		$this->smarty->assign('ptype_firsts', $ptype_firsts);
		$this->smarty->assign('title', '添加产品');
		$this->smarty->display('admin/product/edit.tpl');
	}
	
	function edit_submit(){
		$_POST['pid'] = intval($_POST['pid']);
		
		if($_POST['pid']){
			$this->taxonomyAction->termDataDeleteByPid($_POST['pid']);
			$this->productAction->productUpdate($_POST);
			$pid = $_POST['pid'];
		}else{
			$pid = $this->productAction->productAdd($_POST);
			if(!$pid){
			    $result = array('pid' => '-1');
			    exit(json_encode($result));
			}
		}
		$this->taxonomyAction->termDataAdd($_POST['tid'], $pid);
		$imgData = $this->imageAction->imageAdd($pid);
		
		$result = array('pid' => $pid);
		if($imgData){
    		foreach($imgData as $tmp){
    		    $tmp['type'] == 'thumbnail' && $result['thumbnail'] = $tmp['path'];
    		    $tmp['type'] == 'image' && $result['image'] = $tmp['path'];
    		}
		}
		
		exit(json_encode($result));
	}
	
	function delete(){
		$pid = intval($_REQUEST['pid']);
		if($pid){
			$return = $this->productAction->productDel($pid);
			exit($return? '1': '-1');
		}else{
			exit('-2');
		}
	}
	function get_data_remote(){
		
		$fields = array(
			'source' => '请选择商品来源',
			'source_id' => '源商品id不能为空',
		);
		$cp = check_post(array_keys($fields));
		if(!($cp === '1')){
			exit($fields[$cp]);
		}
		
		$source = intval($_POST['source']);
		$source_id = trim($_POST['source_id']);
		$source = intval($_REQUEST['source']);
		$source_id = trim($_REQUEST['source_id']);
		
		$data = $this->productAction->get_data_remote($source, $source_id);
		
		exit(json_encode($data));
	}
    //查询数据库，关联关键字的结果
    function search(){
        $pWord = intval($_REQUEST['searchWord']);
        $productListSearch = $this->productAction->productListSearch($pWord);
        $this->smarty->assign('productListSearch', $productListSearch);
        $this->smarty->display('portal/product/search.tpl');
    }
}